package com.uniplaza.app.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Type;

/**
 * 本地存储 配置文件工具类
 * Created by Kim on 16/6/6.
 */
public class ConfigUtils {
    private static ConfigUtils instance;
    private SharedPreferences sp;

    public static ConfigUtils getInstance(Context context, String name) {
        if (instance == null) {
            instance = new ConfigUtils();
        }
        instance.sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        return instance;
    }

    // public ConfigUtils(Context context) {
    // sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
    // }

    /**
     * 获取存储String 类型值
     *
     * @param key 获取值的 key
     * @return 获取的值
     */
    public String getString(String key) {
        return sp.getString(key, "");
    }

    /**
     * 存储 String 类型的值
     *
     * @param key   存储的key
     * @param value 存储的值
     */
    public void setString(String key, String value) {
        sp.edit().putString(key, value).apply();
    }

    /**
     * 获取存储 boolean 类型值
     *
     * @param key 获取值的 key
     * @return 获取的值
     */
    public boolean getBoolean(String key) {
        return sp.getBoolean(key, false);
    }

    /**
     * 存储 boolean 类型的值
     * 1c67bac9-d884-3a16-b903-18cf238c7167
     *
     * @param key   存储的key
     * @param value 存储的值
     */
    public void setBoolean(String key, boolean value) {
        sp.edit().putBoolean(key, value).apply();
    }


    /**
     * 针对复杂类型存储<对象>
     *
     * @param key
     * @param object
     */
    public void setObject(String key, Object object) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream out = null;
        try {

            out = new ObjectOutputStream(baos);
            out.writeObject(object);
            String objectVal = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
            SharedPreferences.Editor editor = sp.edit();
            editor.putString(key, objectVal);
            editor.apply();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                baos.close();
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @SuppressWarnings("unchecked")
    public <T> T getObject(String key, Type type) {
        if (sp.contains(key)) {
            String objectVal = sp.getString(key, null);
            byte[] buffer = Base64.decode(objectVal, Base64.DEFAULT);
            ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(bais);
                T t = (T) ois.readObject();
                return t;
            } catch (Exception e) {
                Utils.logError("Error", e.toString());
                e.printStackTrace();
            } finally {
                try {
                    bais.close();
                    if (ois != null) {
                        ois.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

}
